package com.example.bangbangxia.utils;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * 发送短信验证码
 */
public class SendSmsUtils {

    private static final String accessKeyId="LTAI4FkNxrgci7ze3eX4QDd3";
    private static final String accessSecret="D1aBVqT9prqVXj33rFEXhnEizwbJft";

    public static int send(String userPhone,String code){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", userPhone);
        request.putQueryParameter("SignName", "帮帮侠");
        request.putQueryParameter("TemplateCode", "SMS_187270933");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            String Message = response.getData().substring(12, 14);//切出Message，看是不是返回OK
            System.out.println("Message:"+Message);
            if (Message.equals("OK")){
                return 1;
            }else{
                return 0;
            }
        } catch (ServerException e) {
            e.printStackTrace();
            return 0;
        } catch (ClientException e) {
            e.printStackTrace();
            return 0;
        }
    }

}
